package com.sks.web.action.house;

import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.stereotype.Controller;

import com.sks.bean.PageView;
import com.sks.bean.pojo.Area;
import com.sks.bean.pojo.House;
import com.sks.bean.pojo.HouseType;
import com.sks.bean.pojo.PropertyType;
import com.sks.bean.pojo.SksInfo;
import com.sks.bean.privilege.Employee;
import com.sks.core.SystemConstants;
import com.sks.service.base.SksInfoService;
import com.sks.service.house.AreaService;
import com.sks.service.house.HouseService;
import com.sks.service.house.HouseTypeService;
import com.sks.service.house.PropertyTypeService;
import com.sks.service.news.NewsService;
import com.sks.service.privilege.EmployeeService;
import com.sks.web.action.BasicAction;

@Controller("/sks")
public class IndexAction extends BasicAction {
	@Resource(name = "houseServiceBean") private HouseService service;
	@Resource(name = "houseTypeServiceBean") private HouseTypeService houseTypeService;
	@Resource(name = "areaServiceBean") private AreaService areaService;
	@Resource(name = "newsServiceBean") private NewsService newsService;
	@Resource(name = "propertyTypeServiceBean") private PropertyTypeService ptService;
	@Resource(name = "sksInfoServiceBean") private SksInfoService sksService;
	@Resource(name = "employeeServiceBean") private EmployeeService employeeService;
	
	public ActionForward index(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Area> areas = areaService.findTopArea(true);
		List<HouseType> hts = houseTypeService.getScrollData().getResultlist();
		List<PropertyType> ppts = ptService.getScrollData(-1, -1).getResultlist();
		
		PageView<House> pv = new PageView<House>(5, 1);
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("isHot", "desc");
		orderby.put("publishTime", "asc");
		List<House> hs = service.getScrollData(pv.getFirstResult(), pv.getMaxresult(), "o.houseStatus = '" + House.HOUSE_STATUS_PUBLISHED + "'",
				null, orderby).getResultlist();
		List<Employee> employee = employeeService.getScrollData(0, 3).getResultlist();
		request.setAttribute("tops", areas);
		request.setAttribute("hts", hts);
		request.setAttribute("hs", hs);
		request.setAttribute("employee", employee);
		request.setAttribute("ppts", ppts);
		
		LinkedHashMap<String, String> order = new LinkedHashMap<String, String>();
		order.put("click", "desc");
		request.setAttribute("news", newsService.getScrollData(0, 8, "o.status = 'published'", null, order).getResultlist());
		
		return mapping.findForward("index");
	}
	
	public ActionForward about(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		SksInfo si = sksService.getAboutUs();
		if (null != si) {
			request.setAttribute("sksInfo", si.getInfo());
		}
		request.setAttribute("sksKey", SystemConstants.SKS_ABOUT_US);
		return mapping.findForward("sksInfo");
	}
	
	public ActionForward intro(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		SksInfo si = sksService.getSksIntroduce();
		if (null != si) {
			request.setAttribute("sksKey", SystemConstants.SKS_INTRODUCE);
			request.setAttribute("sksInfo", si.getInfo());
		}
		return mapping.findForward("sksInfo");
	}
	
}
